package com.ktg.mes.md.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ktg.common.core.domain.BaseEntity;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.mes.md.mapper.MdLabelPrintMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.md.domain.MdLabelPrint;
import com.ktg.mes.md.service.IMdLabelPrintService;



/**
 * 产品标签打印Service业务层处理
 *
 * @author byteco
 * @date 2022-12-08
 */
@Service
public class MdLabelPrintServiceImpl extends ServiceImpl<MdLabelPrintMapper, MdLabelPrint> implements IMdLabelPrintService
{




    /**
     * 查询产品标签打印
     *
     * @param entity 产品标签打印
     * @param page 分页对象
     * @return 产品标签打印
     */
    @Override
    public IPage<MdLabelPrint> queryPageList(MdLabelPrint entity, Page<MdLabelPrint> page) {
        // QueryWrapper<MdLabelPrint> lqw = new QueryWrapper<MdLabelPrint>(entity);
        LambdaQueryWrapper<MdLabelPrint> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(BaseEntity::getCreateTime);
        IPage<MdLabelPrint> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询产品标签打印
     *
     * @param entity 产品标签打印
     * @return 产品标签打印
     */
    @Override
    public List<MdLabelPrint> queryList(MdLabelPrint entity) {
        QueryWrapper<MdLabelPrint> lqw = new QueryWrapper<MdLabelPrint>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public MdLabelPrint getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增产品标签打印
     *
     * @param entity 产品标签打印
     * @return 结果
     */
    @Override
    public void insert(MdLabelPrint entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        // 获取上一次流水号结束
        LambdaQueryWrapper<MdLabelPrint> wrapper = new LambdaQueryWrapper<>();
        // wrapper.apply("create_time = (select max(create_time) from md_label_print)");
        wrapper.apply("end_serial_number = (select max(end_serial_number) from md_label_print)");
        MdLabelPrint last_print = this.getOne(wrapper);
        if(ObjectUtils.isNotNull(last_print)){
            Integer endSerialNumber = last_print.getEndSerialNumber(); //上一次打印 结束流水号
            entity.setStaSerialNumber(endSerialNumber +1); // 开始流水号
            entity.setEndSerialNumber(entity.getStaSerialNumber() + entity.getPrintQuantity()); // 结束流水号
        }else{
            entity.setStaSerialNumber(0);
            entity.setEndSerialNumber(entity.getPrintQuantity());
        }
        this.save(entity);
    }

    /**
     * 修改产品标签打印
     *
     * @param entity 产品标签打印
     * @return 结果
     */
    @Override
    public boolean update(MdLabelPrint entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除产品标签打印
     *
     * @param ids 需要删除的产品标签打印主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] ids)
    {

        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除产品标签打印信息
     *
     * @param id 产品标签打印主键
     * @return 结果
     */
    @Override
    public boolean delete(Long id)
    {
        return this.removeById(id);
    }

    private LambdaQueryWrapper<MdLabelPrint> buildQueryWrapper(MdLabelPrint entity) {
        LambdaQueryWrapper<MdLabelPrint> lqw = Wrappers.lambdaQuery();
                    lqw.like(StringUtils.isNotBlank(entity.getItemCode()), MdLabelPrint::getItemCode, entity.getItemCode());
                    lqw.like(StringUtils.isNotBlank(entity.getItemName()), MdLabelPrint::getItemName, entity.getItemName());
                    lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), MdLabelPrint::getSpecification, entity.getSpecification());
                    lqw.eq(StringUtils.isNotBlank(entity.getUnitOfMeasure()), MdLabelPrint::getUnitOfMeasure, entity.getUnitOfMeasure());
                    lqw.eq(StringUtils.isNotBlank(entity.getBatchNo()), MdLabelPrint::getBatchNo, entity.getBatchNo());
                    lqw.eq(StringUtils.isNotBlank(entity.getPrefix()), MdLabelPrint::getPrefix, entity.getPrefix());
                    lqw.eq(entity.getPrintQuantity() != null, MdLabelPrint::getPrintQuantity, entity.getPrintQuantity());
        return lqw;
    }
}
